# -*- coding: utf-8 -*-

"""
@author: 猿小天
@contact: QQ:1638245306
@Created on: 2021/6/3 003 0:30
@Remark: 用户管理
"""
import hashlib

from django.contrib.auth.hashers import make_password
from rest_framework import serializers
from rest_framework.permissions import IsAuthenticated

from ops.config.models import Service
from dvadmin.utils.json_response import SuccessResponse, ErrorResponse
from dvadmin.utils.serializers import CustomModelSerializer
from dvadmin.utils.validator import CustomUniqueValidator
from dvadmin.utils.viewset import CustomModelViewSet




class ServiceSerializer(CustomModelSerializer):
    """
    服务管理-序列化器
    """

    class Meta:
        model = Service
        read_only_fields = ["id"]
        fields = "__all__"

class ServiceCreateUpdateSerializer(CustomModelSerializer):
    """
    服务管理-序列化器
    """
    mark = serializers.CharField(max_length=50,
                                     validators=[CustomUniqueValidator(queryset=Service.objects.all(), message="环境标识必须唯一")])

    class Meta:
        model = Service
        read_only_fields = ["id"]
        fields = "__all__"



class ServiceViewSet(CustomModelViewSet):
    """
    服务管理接口
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    permission_classes = [IsAuthenticated]
    queryset = Service.objects.all()
    serializer_class = ServiceSerializer
    search_fields = ['name', 'mark']
    create_serializer_class = ServiceCreateUpdateSerializer
    update_serializer_class = ServiceCreateUpdateSerializer


